import { createRouter, createWebHistory } from "vue-router";

import register from '@/views/Register.vue'
import login from '@/views/Login.vue'
import manage from '@/views/Manage.vue'
import person from '@/views/Person.vue'
import user from '@/views/User.vue'
import carboner from '@/views/Carboner.vue'
import task from '@/views/Task.vue'
import activity from '@/views/Activity.vue'
import taskDetail from '@/views/TaskDetail.vue'
import taskCheck from '@/views/TaskCheck.vue'
import donation from '@/views/Donation.vue'
import exchange from '@/views/Exchange.vue'

const router = createRouter({
    history: createWebHistory(),
    routes:[
        {
            path: '/manage',
            name:'用户管理',
            component: manage,
            redirect: "/manage/user",
            children: [
                {path: 'user', name:'用户管理/用户列表', component: user},
                {path: 'carboner', name: '碳行者管理/', component: carboner},
                {path: 'person',  name: '用户管理/个人中心', component: person},
                {path: 'task', name: '任务管理/', component: task},
                {path: 'activity', name: '发布活动/', component: activity},
                {path: 'taskDetail', name: '任务管理/任务详情', component: taskDetail},
                {path: 'taskCheck', name: '任务管理/任务审核', component: taskCheck},
                {path: 'exchange', name: '物物交换管理/物品交换', component: exchange},
                {path: 'donation', name: '物物交换管理/物品捐赠', component: donation}
            ]
        },
        {path:'/register', name: '注册', component: register},
        {path: '/login', name:'登录',  component: login},
        {path: '/', component: login}
    ]
})

import { pathInfo } from "@/store/pathInfo"

router.beforeEach((to, from, next) => {
    const pathStore = pathInfo()
    pathStore.currentPathName = to.name
    pathStore.currentPath = to.path
    const user = JSON.parse(localStorage.getItem('user'))
    if(!user && to.path !== '/register'){
        if(to.path !== '/login'){
            next('/login')
        }
    }
    next()
})

export default router